package com.orkasgb.framework.hadoop.mapperreduce.join.reducejoin;

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;

public class JoinDriver {

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        // 获取配置
        Configuration configuration = new Configuration();

        //获取job
        Job job = Job.getInstance(configuration);

        // 设置Mapper和Reduce
        job.setMapperClass(JoinMapper.class);
        job.setReducerClass(JoinReduce.class);

        // 设置Mapper输出k/v
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(JoinEntity.class);

        // 设置最终输出k/v
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(JoinEntity.class);

        FileUtils.deleteDirectory(new File("/home/huanzi/hadoop/output/join/reducejoin"));

        // 设置输入输出文件路径
        FileInputFormat.setInputPaths(job, new Path("/home/huanzi/hadoop/input/join/order.txt"), new Path("/home/huanzi/hadoop/input/join/product.txt"));
        FileOutputFormat.setOutputPath(job, new Path("/home/huanzi/hadoop/output/join/reducejoin"));

        // 提交job
        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);

    }
}
